<?php

/**
 * This is the model class for table "tbl_dispatchparts".
 *
 * The followings are the available columns in table 'tbl_dispatchparts':
 * @property string $dispatch
 * @property integer $counter
 * @property string $warehouse
 * @property string $partno
 * @property string $partdesc
 * @property double $quanity
 * @property integer $noprint
 * @property double $price
 * @property string $jobclass
 */
class Dispatchparts extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Dispatchparts the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_dispatchparts';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('dispatch,warehouse, partno, partdesc, quanity, noprint, price', 'required'),
			array('noprint', 'numerical', 'integerOnly'=>true),
			array('quanity, price', 'numerical'),
			array('dispatch', 'length', 'max'=>15),
			array('warehouse', 'length', 'max'=>4),
			array('partno', 'length', 'max'=>75),
			array('jobclass', 'length', 'max'=>40),		
			array('added_date,added_by','safe'),				
			array('dispatch, counter, warehouse, partno, partdesc, quanity, noprint, price, jobclass', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'employee'=>array(self::BELONGS_TO,'Employees','added_by'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'dispatch' => 'Dispatch',
			'counter' => 'Counter',
			'warehouse' => 'Warehouse',
			'partno' => 'Partno',
			'partdesc' => 'Partdesc',
			'quanity' => 'Quanity',
			'noprint' => 'Noprint',
			'price' => 'Price',
			'jobclass' => 'Jobclass',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('dispatch',$this->dispatch,true);
		$criteria->compare('counter',$this->counter);
		$criteria->compare('warehouse',$this->warehouse,true);
		$criteria->compare('partno',$this->partno,true);
		$criteria->compare('partdesc',$this->partdesc,true);
		$criteria->compare('quanity',$this->quanity);
		$criteria->compare('noprint',$this->noprint);
		$criteria->compare('price',$this->price);
		$criteria->compare('jobclass',$this->jobclass,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	public function beforeValidate(){
		
		if($this->isNewRecord){
			$this->getNextCounter();
			$this->added_date = DateUtil::getDefaultCurrentDateTime();	
			if(Yii::app()->user->isGuest == false){
				$this->added_by = Yii::app()->user->model->employeeid;
			}
		}
		return parent::beforeValidate();
	}
    public function getNextCounter(){
        if($this->isNewRecord){
            $num = Yii::app()->db->createCommand('select max(counter) from tbl_dispatchparts where dispatch=:dispatch')
                    ->bindValues(array(
                        'dispatch'=>$this->dispatch
                    ))->queryScalar();
            $this->counter = str_pad($num + 1, 3, '0', STR_PAD_LEFT);
        }else{
            return $this->counter;
        }
    }
    public function getWarehouseList(){
        $data=Yii::app()->db->createCommand('select * from tbl_warehouse')->queryAll();
        $rs=array(
           
        );
        
        if(count($data)>0){
            foreach($data as $i=>$item){
                $rs[$item['warehouseid']]=$item['warehousedesc'];
            }
        }
        return $rs;
    }
    
    public function getWarehouseText(){
        $data=Yii::app()->db->createCommand('select warehousedesc from tbl_warehouse where warehouseid=:id')->bindValue('id',$this->warehouse)->queryScalar();
        if($data!=false){
            return $data;
        }
        return NULL;
    }
}